package com.ljj.controller;

import com.ljj.VO.ResultVO;
import com.ljj.converter.OrderForm2OrderDTOConverter;
import com.ljj.dto.OrderDTO;
import com.ljj.enums.ResultEnum;
import com.ljj.exception.SellException;
import com.ljj.form.OrderForm;
import com.ljj.service.IBuyerService;
import com.ljj.service.IOrderService;
import com.ljj.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 39716 on 2018/4/6.
 */
@RestController
@Slf4j
@RequestMapping("/buyer/order")
public class BuyerOrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IBuyerService buyerService;

    //创建订单
    @PostMapping("/create")
    public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm,
                                                BindingResult bindingResult) {
        //校验表单是否正确
        if (bindingResult.hasErrors()) {
            log.error("【创建订单】参数不正确, orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        //转换格式
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        //判断订单详情
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        //创建订单
        OrderDTO createResult = orderService.create(orderDTO);
        //返回的数据
        Map<String, String> map = new HashMap<>();
        map.put("orderId", createResult.getOrderId());

        return ResultVOUtil.success(map);
    }

    //查看订单列表
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> findOrderList(@RequestParam("openid") String openid,
                                                  @RequestParam(value = "page", defaultValue = "0") Integer page,
                                                  @RequestParam(value = "size", defaultValue = "10") Integer size) {
        //判断openid不能为空
        if (StringUtils.isEmpty(openid)) {
            log.error("openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        //查询出当前用户订单
        PageRequest pageRequest = new PageRequest(page, size);
        Page<OrderDTO> orderDTOPage = orderService.findList(openid, pageRequest);
        return ResultVOUtil.success(orderDTOPage.getContent());
    }

    //查看订单详情
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId) {
//        //不安全
//        OrderDTO orderDTO = orderService.findOne(orderId);
        OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);
        return ResultVOUtil.success(orderDTO);
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId){
//        //不安全
//        OrderDTO orderDTO = orderService.findOne(orderId);
        buyerService.cancelOrder(openid, orderId);
        return ResultVOUtil.success();
    }
}
